package com.scrabbleMaster;

import java.util.Collection;
import java.util.HashSet;

public class StringCombiner {

	public Collection<String> findCombinations(String value) {
		Collection<String> result = new HashSet<String>();
		this.permuteAndCollect("", value, result);
		return result;
	}

	private void permuteAndCollect(String beginningString, String endingString, Collection<String> result) {
	    if (endingString.length() <= 1) {
	      //System.out.println(beginningString + endingString);
	      result.add(beginningString + endingString);
	    } else
	      for (int i = 0; i < endingString.length(); i++) {
	        try {
	          String newString = endingString.substring(0, i) + endingString.substring(i + 1);
	          result.add(newString);
	          result.add(beginningString + endingString.charAt(i));
	          permuteAndCollect(beginningString + endingString.charAt(i), newString, result);
	        } catch (StringIndexOutOfBoundsException exception) {
	          exception.printStackTrace();
	        }
	      }
	  }
}
